package qj.app.p1;

import static qj.tool.jna.KeyboardHandler.BACKQUOTE;

import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import qj.tool.build.BuildUtil;
import qj.tool.jna.KeyboardHandler;
import qj.ui.DesktopUI4;
import qj.util.Clipboard;
import qj.util.FileUtil;
import qj.util.ImageUtil;
import qj.util.PropertiesUtil;
import qj.util.ThreadUtil;
import qj.util.funct.F1;
import qj.util.funct.F2;
import qj.util.funct.Fs;
import qj.util.funct.P0;

public class Forms {

	public static class Build {
		public static void main(String[] args) {
			BuildUtil.buildApp(Forms.class);
		}
	}
	public static class Test {
		public static void main(String[] args) {
			System.out.println("[5:38:38 PM] EugEnE:     Serial ".replaceAll("(?m)^\\[\\d+:\\d+:\\d+ \\w+\\]\\s+\\w+:\\s*", ""));
		}
	}
	
	static final Properties config = PropertiesUtil.loadPropertiesFromFile(new File("forms_config.properties"));
	
	public static void main(String[] args) {	

		final String ic1 = config.getProperty("ic1");
		final String ic2 = config.getProperty("ic2");
		final String ic3 = config.getProperty("ic3");

		Mapper mapper = mapper(BACKQUOTE);

		mapper.map('L', new P0() {public void e() {
			Clipboard.type("tester");
			DesktopUI4.type('\t');
			Clipboard.type("packetone");
			DesktopUI4.type('\n');
		}});
		mapper.map(new int[] {'D','1'}, new P0() {public void e() {
			FileUtil.writeToFile("", "d:/Quan/Apps/jboss-as-7.0.2.Final/standalone/deployments/P1EAR.ear.dodeploy");
		}});

		mapper.map(new int[] {'I','P'}, new P0() {public void e() {
			BufferedImage image = Clipboard.getImage();
			if (image==null) {
				return;
			}
			try {
				File tempFile = File.createTempFile("ScreenShot", ".png");
				ImageUtil.writeToFile(image, tempFile);
				Clipboard.copy(tempFile.getAbsolutePath());
				tempFile.deleteOnExit();
			} catch (IOException e1) {
				throw new RuntimeException(e1);
			}
		}});
		
		mapper.map(new int[] {'D','3'}, new P0() {public void e() {
			
			String text = Clipboard.getText();
			Clipboard.type("System.out.println(\"" + text + ": \" + (" + text + "));");
		}});
		mapper.map('M', new P0() {public void e() {
			Clipboard.type(Clipboard.getText().replaceFirst("00", "MAC"));
		}});
		mapper.map('V', new P0() {public void e() {
			String text = Clipboard.getText();
			for (String line : text.split("\r?\n")) {
				Clipboard.type(line);
				DesktopUI4.type('\t');
				DesktopUI4.type('\t');
				Clipboard.type(line.replaceFirst("00", "MAC"));
				DesktopUI4.type('\t');
			}
		}});
		// Skype
		mapper.map(new int[] {'S','K'}, new P0() {public void e() {
			Clipboard.type(Clipboard.getText().replaceAll("(?m)^\\[\\d+:\\d+:\\d+ \\w+\\]\\s+[^:]+:\\s*", ""));
		}});
		
		mapper.map(KeyboardHandler.COMMA, new P0() {public void e() {
			Clipboard.copy(Clipboard.getText().replaceAll("\r?\n", ","));
		}});
		
		mapper.map(new int[] {'L','2'}, new P0() {public void e() {
			Clipboard.type("p1n0522");
			DesktopUI4.type('\t');
			Clipboard.type("packetone");
			DesktopUI4.type('\n');
		}});

		mapper.map('C', new P0() {public void e() {
			Clipboard.type(ic1 + "-" + ic2 + "-" + ic3);
		}});
		mapper.map('R', new P0() {public void e() {
			Clipboard.type("C9999.99999");
		}});
		mapper.map(new int[] {'S', '1'}, new P0() {public void e() {
			Clipboard.type("TESTUH00006010");
			DesktopUI4.type('\t');
			DesktopUI4.type('\t');
			Clipboard.type("TESTUHMAC006010");
		}});
		mapper.map(new int[] {'S', '2'}, new P0() {public void e() {
			Clipboard.type("TESTDV00000009");
			DesktopUI4.type('\t');
			DesktopUI4.type('\t');
			Clipboard.type("TESTDVMAC000009");
		}});
		mapper.map(new int[] {'S', '3'}, new P0() {public void e() {
			Clipboard.type("UATDV00000300");
			DesktopUI4.type('\t');
			DesktopUI4.type('\t');
			Clipboard.type("UATDVMAC000300");
		}});
		mapper.map(new int[] {'S', '6'}, new P0() {public void e() {

			Clipboard.type(config.getProperty("s.6.1.ser"));
			DesktopUI4.type('\t');
			DesktopUI4.type('\t');
			Clipboard.type(config.getProperty("s.6.1.mac"));
			DesktopUI4.type('\t');
			Clipboard.type(config.getProperty("s.6.2.ser"));
			DesktopUI4.type('\t');
			DesktopUI4.type('\t');
			Clipboard.type(config.getProperty("s.6.2.mac"));
			DesktopUI4.type('\t');
			Clipboard.type(config.getProperty("s.6.3.ser"));
			DesktopUI4.type('\t');
			DesktopUI4.type('\t');
			Clipboard.type(config.getProperty("s.6.3.mac"));
		}});
		
		mapper.map(new int[] {'A'}, new P0() {public void e() {
			Clipboard.type("green packet");
			DesktopUI4.type('\t');
			DesktopUI4.type('\t');
			DesktopUI4.type('\t');
			Clipboard.type("Jalan Templer");
			DesktopUI4.type('\t');
			DesktopUI4.type('\t');
			Clipboard.type("46050");
			DesktopUI4.type('\t');
			Clipboard.type("Pj");
			DesktopUI4.type('\t');
			DesktopUI4.type('S');
			DesktopUI4.type('S');
			DesktopUI4.type('S');
			DesktopUI4.type('\t');
			DesktopUI4.type(KeyEvent.VK_DOWN);
		}});

		mapper.map(new int[] {'A', '2'}, new P0() {public void e() {
			Clipboard.type("green packet,Jalan Templer");
			DesktopUI4.type('\t');
			DesktopUI4.type('\t');
			DesktopUI4.type('\t');
			Clipboard.type("Pj");
			DesktopUI4.type('\t');
			DesktopUI4.type('S');
			DesktopUI4.type('S');
			DesktopUI4.type('S');
			DesktopUI4.type('\t');
			Clipboard.type("46050");
			DesktopUI4.type('\t');
			DesktopUI4.type(KeyEvent.VK_DOWN);
		}});
		
		mapper.map(new int[] {'A', '1'}, new P0() {public void e() {
//			Fs.repeat(6, DesktopUI4.typeF('\t')).e();
			DesktopUI4.type(KeyEvent.VK_DOWN);
			DesktopUI4.type('\t');
			DesktopUI4.type(KeyEvent.VK_DOWN);
			Fs.repeat(7, DesktopUI4.typeF('\t')).e();
			Clipboard.type("Quan");
			Fs.repeat(13, DesktopUI4.typeF('\t')).e();
			Clipboard.type(config.getProperty("phone"));
		}});
		mapper.map(new int[] {'D'}, new P0() {public void e() {

			DesktopUI4.type('\t');
			DesktopUI4.type(KeyEvent.VK_DOWN);
			DesktopUI4.type('\t');
			DesktopUI4.type(KeyEvent.VK_DOWN);
			DesktopUI4.type('\t');
			Clipboard.type(config.getProperty("cust.name"));
			DesktopUI4.type('\t');
			DesktopUI4.type('\t');
			
			// Saleman
//			Clipboard.type(ic1);
			DesktopUI4.type('\t');
//			Clipboard.type(ic2);
			DesktopUI4.type('\t');
//			Clipboard.type(ic3);
			DesktopUI4.type('\t');
			DesktopUI4.type('\t');
			DesktopUI4.type(KeyEvent.VK_DOWN);
			DesktopUI4.type('\t');
			DesktopUI4.type(KeyEvent.VK_DOWN);
			DesktopUI4.type('\t');
			DesktopUI4.type(KeyEvent.VK_DOWN);
			DesktopUI4.type('\t');
			DesktopUI4.type('\t');
			Clipboard.type("03123123123");
			DesktopUI4.type('\t');
			Clipboard.type(config.getProperty("phone"));
			DesktopUI4.type('\t');
			Clipboard.type("03123123123");
			DesktopUI4.type('\t');
			DesktopUI4.type('\t');
			Clipboard.type("03123123123");
			DesktopUI4.type('\t');
			Clipboard.type(config.getProperty("cust.email"));
			DesktopUI4.type('\t');
			DesktopUI4.type(KeyEvent.VK_DOWN);
			DesktopUI4.type(KeyEvent.VK_DOWN);
			DesktopUI4.type('\t');
			
			// Billing
			DesktopUI4.type(KeyEvent.VK_DOWN);
		}});
		mapper.map('U', new P0() {public void e() {
			Clipboard.type("http://localhost:9080/ePOS/store-customer/verifyProcess.do?accountType=P&actionValue=ctos&searchCategory=1&tempName=Qq&tempDobDisplay=10/12/1972&searchString=" + ic() + "&debug=aa");
		}});
		mapper.map(new int[] {'P'}, new P0() {public void e() {
			// Saleman
			Clipboard.type(ic1);
			DesktopUI4.type('\t');
			Clipboard.type(ic2);
			DesktopUI4.type('\t');
			Clipboard.type(ic3);
			
			DesktopUI4.type('\t');
			Clipboard.type("Quan");
			
		}});
	}
	
	public static class Task {
		public int[] btns;
		public P0 action;
	}
	public static Mapper mapper(int modKey) {
		final boolean[] waitingCommand = {false};
		
		final List<Task> list = new LinkedList<Task>();
		
		KeyboardHandler.mapAll(new F2<Integer,Boolean, Boolean>() {public Boolean e(Integer key, Boolean pressed) {
			if (waitingCommand[0]) {
				
				if (!pressed) {
					AA:
					for (Task task : list) {
						int[] btns = task.btns;
						if (key == btns[btns.length - 1]) {
							for (int i = 0; i < btns.length - 1; i++) {
								if (!KeyboardHandler.isPressed(btns[i])) {
									continue AA;
								}
							}

							waitingCommand[0] = false;
							ThreadUtil.run(task.action);
							
							return false;
						}
					}
					
				}
				
				return false;
			}
			return true;
		}});
		KeyboardHandler.map(modKey, new F1<Boolean, Boolean>() {public Boolean e(Boolean obj) {
			waitingCommand[0] = obj;
			return false;
		}});
		return new Mapper(){
			public void map(int btn, final P0 p0) {
				map(new int[] {btn}, p0);
			}
			
			public void map(final int[] btns, final P0 p0) {
				Task task = new Task();
				task.btns = btns;
				task.action = p0;
				list.add(task);
			}
		};
	}

	private static String ic() {
		return config.getProperty("ic1") + "-" + 
				config.getProperty("ic2") + "-" + 
				config.getProperty("ic3")
				;
	}
	public static interface Mapper {
		void map(int[] btn, P0 p0);
		void map(int btn, P0 p0);
	}
}
